package educrypt.gui.components;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import javax.swing.JTextField;

import educrypt.gui.view.EducryptFileChooserView;
import educrypt.gui.view.TextView;
import educrypt.i18n.Messages;

class TextPanel extends EducryptPanel implements TextView {

	private static final long serialVersionUID = 6983644680679626951L;

	private EducryptFileChooser loadFileDialog;
	private JTextField tfTextPath;
	private JButton btnBrowse;
	private JButton btnPreview;

	private JLabel lblFile, lblTextPath;

	public TextPanel() {
		this.setSize(750, 80);
		this.setPreferredSize(this.getSize());

		setLayout(null);

		lblFile = new JLabel(Messages.getString("guiFile"));
		lblFile.setBounds(31, 11, 64, 19);
		lblFile.setFont(new Font("Tahoma", Font.BOLD, 15));
		add(lblFile);

		btnBrowse = new JButton(Messages.getString("guiBrowse"));
		btnBrowse.setBounds(476, 38, 100, 23);
		add(btnBrowse);

		lblTextPath = new JLabel(Messages.getString("guiFilePath"));
		lblTextPath.setBounds(31, 42, 64, 14);
		add(lblTextPath);

		tfTextPath = new JTextField();
		tfTextPath.setEditable(false);
		tfTextPath.setBounds(100, 38, 372, 23);
		add(tfTextPath);
		tfTextPath.setColumns(10);

		JSeparator separator = new JSeparator();
		separator.setBounds(195, 78, 359, 1);
		add(separator);

		btnPreview = new JButton(Messages.getString("guiPreview"));
		btnPreview.setBounds(604, 38, 100, 23);
		btnPreview.setEnabled(false);
		add(btnPreview);

		loadFileDialog = new EducryptFileChooser(this);
		loadFileDialog.setTextMode();
	}

	@Override
	public void addListener(ActionListener ctr) {
		btnBrowse.setActionCommand(FILE_BROWSE);
		btnBrowse.addActionListener(ctr);
		tfTextPath.setActionCommand(PREVIEW);
		tfTextPath.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				showFileChooser();
			}
		});

		btnPreview.setActionCommand(PREVIEW);
		btnPreview.addActionListener(ctr);
		loadFileDialog.addListener(ctr);
	}

	@Override
	public void showFileChooser() {
		loadFileDialog.showOpenDialog();
	}

	@Override
	public void setCipherContext() {

	}

	@Override
	public void setCryptanalysisContext() {

	}

	@Override
	public void setFilePath(String path) {
		tfTextPath.setText(path);
	}

	@Override
	public void enablePreview(boolean flag) {
		btnPreview.setEnabled(flag);
	}

	@Override
	public EducryptFileChooserView getFileChooser() {
		return loadFileDialog;
	}
}
